package com.atguigu.spzx.manager.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.spzx.manager.listener.CategoryExcelListener;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.manager.service.CategoryService;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.vo.product.CategoryExcelVo;
import com.atguigu.spzx.service.exception.GuliException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: CategoryServiceImpk
 * Package: com.atguigu.spzx.manager.service.Impl
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/4 14:52
 * @Version 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    /**
     * 根据id查询子分类列表
     * @param parentId
     * @return
     */
    @Override
    public List<Category> findByParentId(Long parentId) {
        List<Category> list = categoryMapper.findByParentId(parentId);
        // 给hasChildren赋值
        for (Category category : list) {
            Integer count = categoryMapper.findChildrenCountById(category.getId());
            if(count > 0){
                category.setHasChildren(true);
            }else {
                category.setHasChildren(false);
            }
        }
        return list;
    }

    /**
     * 分类excel的下载
     */
    @Override
    public void exportData(HttpServletResponse response) {
        try {
            // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("分类excel", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            List<Category> categories = categoryMapper.findAll();
            List<CategoryExcelVo> categoryExcelVos = categories.stream().map(ele -> {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtils.copyProperties(ele, categoryExcelVo);
                return categoryExcelVo;
            }).collect(Collectors.toList());
            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class)
                    .sheet("分类excel")
                    .doWrite(categoryExcelVos);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 导入分类的excel文件
     * @param file
     */
    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), CategoryExcelVo.class,
                    new CategoryExcelListener<CategoryExcelVo>(categoryMapper)).sheet(0).doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliException(201,"文件上传失败");
        }
    }
}
